package com.tian.generator.util;

import com.tian.common.utils.StringUtils;
import com.tian.generator.config.GenConfig;
import com.tian.generator.domain.*;

import java.io.File;
import java.util.*;

public class GenUtils {

    /**
     * 获取ftls路径
     */
    private static String path = new File(GenUtils.class.getClassLoader().getResource("ftls").getFile()).getPath();
    /**
     * mybatis空间路径
     */
    private static final String MYBATIS_PATH = "main/resources/mapper";

    /**
     * html空间路径
     */
    private static final String TEMPLATES_PATH = "main/resources/html";

    /**
     * 列转换成java属性字段
     */
    public static List<ColumnJava> columsToJava(List<ColumnInfo> columns) {
        // 列信息
        List<ColumnJava> list = new ArrayList<>();
        for (ColumnInfo column : columns) {
            ColumnJava columnJava = new ColumnJava();
            // 列名转换成Java属性名
            columnJava.setColumnName(column.getColumnName());
            columnJava.setAttrName(StringUtils.convertToCamelCase(column.getColumnName(), "_", true));
            columnJava.setAttrDescribe(column.getColumnComment());
            columnJava.setAttrname(StringUtils.convertToCamelCase(column.getColumnName(), "_", false));
            // 列的数据类型，转换成Java类型
            columnJava.setAttrType(DbJavaType.parseTypeFormSqlType(column.getDataType()));
            list.add(columnJava);
        }
        return list;
    }

    /**
     * 表名转换成Java类名
     */
    public static TableJava tableToJava(TableInfo tableInfo, List<ColumnJava> list) {
        String autoRemovePre = GenConfig.getAutoRemovePre();
        TableJava tableJava = new TableJava();
        //去除前缀
        if (autoRemovePre.equals("true")) {
            String tablePrefix = GenConfig.getTablePrefix();
            String oldName = tableInfo.getTableName();
            String newName = oldName.replace(tablePrefix, "");
            tableInfo.setTableName(newName);
            tableJava.setTableName(oldName);
        }else {
            tableJava.setTableName(tableInfo.getTableName());
        }
        tableJava.setClassName(StringUtils.convertToCamelCase(tableInfo.getTableName(), "_", true));
        tableJava.setClassDescribe(tableInfo.getTableComment());
        tableJava.setPrimaryKey(list.get(0));
        tableJava.setClassname(StringUtils.convertToCamelCase(tableInfo.getTableName(), "_", false));
        tableJava.setProperties(list);
        return tableJava;
    }

    /**
     * 把TableJava和配置信息整合在一起
     */
    public static CollectJava getCollectJava(TableJava tableJava) {
        CollectJava collectJava = new CollectJava();
        //配置文件信息
        collectJava.setAuthor(GenConfig.getAuthor());
        collectJava.setModuleName(getModuleName(GenConfig.getPackageName()));
        collectJava.setBasePackage(getBasePackage(GenConfig.getPackageName()));
        collectJava.setAllPackage(GenConfig.getPackageName());
        //TableJava信息
        collectJava.setClassName(tableJava.getClassName());
        collectJava.setClassname(tableJava.getClassname());
        collectJava.setCreatDate(new Date());
        collectJava.setPrimaryKey(tableJava.getPrimaryKey());
        collectJava.setProperties(tableJava.getProperties());
        collectJava.setClassDescribe(tableJava.getClassDescribe());
        collectJava.setTableName(tableJava.getTableName());
        return collectJava;
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplates() {
        List<String> templates = new ArrayList<String>();
        templates.add("java/Controller.java.ftl");
        templates.add("java/Domain.java.ftl");
        templates.add("java/Mapper.java.ftl");
        templates.add("java/Service.java.ftl");
        templates.add("java/ServiceImpl.java.ftl");
        templates.add("xml/Mapper.xml.ftl");
        return templates;
    }


    /**
     * 获取文件名
     */
    public static String getFileName(String template, CollectJava collectJava) {
        // 小写类名
        String classname = collectJava.getClassname();
        // 大写类名
        String className = collectJava.getClassName();
        String javaPath = getProjectPath();
        String mybatisPath = MYBATIS_PATH + "/" + collectJava.getModuleName() + "/" + className;
        String htmlPath = TEMPLATES_PATH + "/" + collectJava.getModuleName() + "/" + classname;

        if (template.contains("Controller.java.ftl")) {
            return javaPath + "controller" + "/" + className + "Controller.java";
        }
        if (template.contains("Domain.java.ftl")) {
            return javaPath + "domain" + "/" + className + ".java";
        }
        if (template.contains("Mapper.java.ftl")) {
            return javaPath + "mapper" + "/" + className + "Mapper.java";
        }
        if (template.contains("Service.java.ftl")) {
            return javaPath + "service" + "/" + "I" + className + "Service.java";
        }
        if (template.contains("ServiceImpl.java.ftl")) {
            return javaPath + "service" + "/impl/" + className + "ServiceImpl.java";
        }

        if (template.contains("Mapper.xml.ftl")) {
            return mybatisPath + "Mapper.xml";
        }

        if (template.contains("list.html.vm")) {
            return htmlPath + "/" + classname + ".html";
        }
        if (template.contains("add.html.vm")) {
            return htmlPath + "/" + "add.html";
        }
        if (template.contains("edit.html.vm")) {
            return htmlPath + "/" + "edit.html";
        }
        if (template.contains("sql.vm")) {
            return classname + "Menu.sql";
        }
        return null;
    }

    /**
     * 获取.java文件的生成路径
     */
    public static String getProjectPath() {
        String packageName = GenConfig.getPackageName();
        StringBuffer projectPath = new StringBuffer();
        projectPath.append("main/java/");
        projectPath.append(packageName.replace(".", "/"));
        projectPath.append("/");
        return projectPath.toString();
    }

    /**
     * 获取模块名
     *
     * @param packageName 包名
     * @return 模块名
     */
    public static String getModuleName(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        int nameLength = packageName.length();
        String moduleName = StringUtils.subString(packageName, lastIndex + 1, nameLength);
        return moduleName;
    }

    /**
     * 获取除去模块的的包名称
     *
     * @param packageName
     * @return 除去模块的的包名称
     */
    public static String getBasePackage(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        String basePackage = StringUtils.subString(packageName, 0, lastIndex);
        return basePackage;
    }
}
